package com.shenzhen.horselive.widget;

import android.content.Context;
import android.graphics.Color;
import android.util.AttributeSet;
import android.view.View;
import android.widget.FrameLayout;
import android.widget.ImageView;
import android.widget.TextView;

import androidx.annotation.NonNull;
import androidx.annotation.Nullable;

import com.flqy.baselibrary.utils.ScreenUtil;
import com.shenzhen.horselive.R;
import com.shenzhen.horselive.api.entity.FoodBean;

import java.math.BigDecimal;
import java.util.ArrayList;
import java.util.List;

/**
 * Author by  qianlu
 * Email : zar.l@qq.com
 * Date on 2020/6/6.
 */
public class ShopCarView extends FrameLayout {

    private TextView car_limit, tv_amount;
    public ImageView iv_shop_car;
    public TextView car_badge;
    public View shoprl;
    public int[] carLoc;

    private List<FoodBean> shopCarFoodBean = new ArrayList<>();


    public ShopCarView(@NonNull Context context) {
        super(context);
    }

    public ShopCarView(@NonNull Context context, @Nullable AttributeSet attrs) {
        super(context, attrs);
    }

    @Override
    protected void onLayout(boolean changed, int left, int top, int right, int bottom) {
        super.onLayout(changed, left, top, right, bottom);
        if (iv_shop_car == null) {
            iv_shop_car = findViewById(R.id.iv_shop_car);
            car_badge = findViewById(R.id.car_badge);
            car_limit = findViewById(R.id.car_limit);
            tv_amount = findViewById(R.id.tv_amount);
            shoprl = findViewById(R.id.car_rl);

            carLoc = new int[2];
            iv_shop_car.getLocationInWindow(carLoc);
            carLoc[0] = carLoc[0] + iv_shop_car.getWidth() / 2 - ScreenUtil.dip2px(10);
        }
    }

    public void updateAmount(BigDecimal amount) {
        if (amount.compareTo(new BigDecimal(0.0)) == 0) {
            car_limit.setText("¥20 起送");
            car_limit.setTextColor(Color.parseColor("#a8a8a8"));
            car_limit.setBackgroundColor(Color.parseColor("#535353"));
            findViewById(R.id.car_nonselect).setVisibility(View.VISIBLE);
            findViewById(R.id.amount_container).setVisibility(View.GONE);
            iv_shop_car.setImageResource(R.drawable.ic_car_bg);
        } else if (amount.compareTo(new BigDecimal(20.0)) < 0) {
            car_limit.setText("还差 ¥" + new BigDecimal(20.0).subtract(amount) + " 起送");
            car_limit.setTextColor(Color.parseColor("#a8a8a8"));
            car_limit.setBackgroundColor(Color.parseColor("#535353"));
            findViewById(R.id.car_nonselect).setVisibility(View.GONE);
            findViewById(R.id.amount_container).setVisibility(View.VISIBLE);
            iv_shop_car.setImageResource(R.drawable.ic_car_bg);
        } else {
            car_limit.setText("去结算");
            car_limit.setTextColor(Color.WHITE);
            car_limit.setBackgroundColor(Color.parseColor("#59d178"));
            findViewById(R.id.car_nonselect).setVisibility(View.GONE);
            findViewById(R.id.amount_container).setVisibility(View.VISIBLE);
            iv_shop_car.setImageResource(R.drawable.ic_car_bg);
        }
        tv_amount.setText("¥" + amount);
    }


    public void addFood(FoodBean foodBean) {
        shopCarFoodBean.add(foodBean);
        showBadge(true);
    }

    public void deletFood(FoodBean foodBean) {
        showBadge(false);

    }


    public void showBadge(boolean isAddFood) {
        int total = Integer.parseInt(car_badge.getText().toString().trim());
        if (isAddFood) {
            ++total;
        } else {
            --total;
        }
        if (total > 0) {
            car_badge.setVisibility(View.VISIBLE);
            car_badge.setText(total + "");
        } else {
            car_badge.setVisibility(View.INVISIBLE);
        }
    }


}
